﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;
using VietStar.Library.Commom.Module;

public partial class controls_client_cartupdate : System.Web.UI.UserControl
{
    Entity.ThanhVien clsE_ThanhVien = new VietStar.Library.Entity.ThanhVien();
    Business.ThanhVien clsB_ThanhVien = new VietStar.Library.Business.ThanhVien();
    Entity.HoaDon clsE_HoaDon = new VietStar.Library.Entity.HoaDon();
    Entity.ChiTietHoaDon clsE_ChiTietHoaDon = new VietStar.Library.Entity.ChiTietHoaDon();
    Business.HoaDon clsB_HoaDon = new VietStar.Library.Business.HoaDon();
    Business.ChiTietHoaDon clsB_ChiTietHoaDon = new VietStar.Library.Business.ChiTietHoaDon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user_login_client"] != "")
            {
                Cart cart = (Cart)Session["cart"];
                BindCart(cart);
                LoadThongTinKhachHang();
            }
            
        }
    }
    private void LoadThongTinKhachHang()
    {
        clsE_ThanhVien.TenDangNhap = Session["user_login_client"].ToString();
        DataTable dt = clsB_ThanhVien.SelectPrimarykey(clsE_ThanhVien);
        lbTen.Text = dt.Rows[0]["TenDayDu"].ToString();
        lbDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
        lbEmail.Text = dt.Rows[0]["Email"].ToString();
        lbSoDienThoai.Text = dt.Rows[0]["SoDienThoai"].ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Cart cart = (Cart)Session["cart"];
        foreach (RepeaterItem item in UpdateCart.Items)
        {
            CheckBox remove = (CheckBox)item.FindControl("remove");
            Label id = (Label)item.FindControl("id");
            TextBox quant = (TextBox)item.FindControl("quantity");
            int i = Convert.ToInt32(id.Text);
            int q = Convert.ToInt32(quant.Text);
            if (remove.Checked)
                cart.RemoveItem(i);
            cart.Update(i, q);
        }
        BindCart(cart);
    }

    private void BindCart(Cart cart)
    {
        List<CartItem> list = new List<CartItem>();
        foreach (CartItem item in cart.CartData.Values)
        {
            list.Add(item);
        }
        UpdateCart.DataSource = list;
        UpdateCart.DataBind();
        sum.Text = cart.TotalPrice.ToString("N0");
        VietStar.Library.Commom.Algorithm.Converts converttotext = new VietStar.Library.Commom.Algorithm.Converts();
        lbSumByText.Text = converttotext.ConvertNumberToText(cart.TotalPrice.ToString())+" đồng ./";
    }
    private int laymahd()
    {
        int mahd=0;
        try
        {
             mahd=Convert.ToInt32(clsB_HoaDon.SelectMaxId(clsE_HoaDon).Rows[0][0].ToString());
            
        }
            catch
        {}
        return mahd;
    }
    protected void btnBuy_Click(object sender, EventArgs e)
    {
        bool save = false;
        try
        {
            clsE_HoaDon.Ten = Session["user_login_client"].ToString();
            clsE_HoaDon.NgayLapHD = DateTime.Now;
            clsE_HoaDon.TongTien =Convert.ToDouble(sum.Text);
            clsE_HoaDon.ChietKhau = 0;
            clsE_HoaDon.TrangThai = false;
            save=clsB_HoaDon.Insert(clsE_HoaDon);
            if (save)
            {
                foreach (RepeaterItem liItem in UpdateCart.Items)
                {
                    clsE_ChiTietHoaDon.MaHD=laymahd();
                    clsE_ChiTietHoaDon.SanPham=Convert.ToInt32((liItem.FindControl("Id")as Label).Text);
                    clsE_ChiTietHoaDon.SoLuong = Convert.ToInt32((liItem.FindControl("quantity")as TextBox).Text);
                    Double DonGia = Convert.ToDouble((liItem.FindControl("lbDonGia") as Label).Text); ;
                    clsE_ChiTietHoaDon.ThanhTien = clsE_ChiTietHoaDon.SoLuong * DonGia;
                    save = clsB_ChiTietHoaDon.Insert(clsE_ChiTietHoaDon);                    
                 }
                if (save == true)
                {
                    Alert.Show("Lập hóa đơn thành công , hãy liên lạc trực tiếp với chúng tôi để nhận được hỗ trợ tốt nhất !!!");
                }
                else
                {
                    Alert.Show("Có lỗi xãy ra trong quá trình lập hóa đơn !!!");
                }
            }
        }
        
        catch { }

        Response.Redirect("~/CartDetail.aspx");
    }
}
